﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 小小次朗点餐系统
{
    public partial class usergn1 : Form
    {
        public static int gj = 0;//退出系统还是关闭窗口

        public static DataTable dt1 = new DataTable();
        public static DataTable dt2 = new DataTable();
        public static DataTable dt3 = new DataTable();
        public static DataTable dt4 = new DataTable();
        public static DataTable dt5 = new DataTable();
        public static DataTable dt6 = new DataTable();
        //订单状态
        public static string State;

        //public static string FoodID;
        public usergn1()
        {
            InitializeComponent();
        }
        private void usergn1_Load(object sender, EventArgs e)//画面加载，画面隐藏
        {
            //页面隐藏
            dataGridView1.Hide();
            dataGridView2.Hide();
            dataGridView3.Hide();
            dataGridView4.Hide();
            dataGridView5.Hide();
            panel6.Hide();
            panel5.Hide();
            lab_Unumber.Text = userLogin.suserName;

            dataGridView1.Show();
            this.dataGridView1.Location = new System.Drawing.Point(0, 100);
            this.dataGridView1.Size = new System.Drawing.Size(545, 580);

            string sql = "select * from Merchants";
            DataSet ds = DBHelper.SelectByAdapter(sql);
            dt1 = ds.Tables[0];
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = dt1;
        }


        #region 选择商家的代码
        private void but_Seller_Click(object sender, EventArgs e)
        {
            //其他页面隐藏
            dataGridView2.Hide();
            dataGridView3.Hide();
            dataGridView4.Hide();
            panel6.Hide();
            dataGridView5.Hide();
            panel5.Hide();

            dataGridView1.Show();
            this.dataGridView1.Location = new System.Drawing.Point(0, 100);
            this.dataGridView1.Size = new System.Drawing.Size(545, 580);

            string sql = "select * from Merchants";
            DataSet ds = DBHelper.SelectByAdapter(sql);
            dt1 = ds.Tables[0];
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = dt1;
        }
        private void 进入ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //其他页面隐藏
            dataGridView1.Hide();
            dataGridView3.Hide();
            dataGridView4.Hide();
            panel6.Hide();
            dataGridView5.Hide();
            panel5.Hide();

            dataGridView2.Show();
            this.dataGridView2.Location = new System.Drawing.Point(0, 100);
            this.dataGridView2.Size = new System.Drawing.Size(545, 580);

            int number = dataGridView1.CurrentRow.Index;
            string sql = string.Format($"select * from Food where MeID={dataGridView1.Rows[number].Cells["Column1"].Value}");
            DataSet ds = DBHelper.SelectByAdapter(sql);

            dt2 = ds.Tables[0];
            dataGridView2.AutoGenerateColumns = false;
            dataGridView2.DataSource = dt2;
        }
        private void 加入购物车ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView2.Rows.Count != 0)
            {
                string FoodID = this.dataGridView2.SelectedRows[0].Cells["Column6"].Value.ToString();

                string sql = string.Format($"select MeID from Food where FoodID='{FoodID}'");
                string MeID = DBHelper.ExecuteScalar(sql).ToString();

                string sql1 = string.Format($"select FoodPrice from Food where FoodID='{FoodID}'");
                string ShoppingPrice = DBHelper.ExecuteScalar(sql1).ToString();

                //获取当前登录账户的id
                string sqlsuerID = string.Format($"select UserID from [User] where Unumber='{userLogin.suserName}' and Pwd='{userLogin.spassWord}'");
                string UserID = DBHelper.ExecuteScalar(sqlsuerID).ToString();

                string sql4 = string.Format($"insert into Shopping values('{FoodID}','{MeID}','1','{ShoppingPrice}','{UserID}')");
                bool result = DBHelper.GetNonQuery(sql4);
                if (result)
                {
                    MessageBox.Show("添加成功");
                }
            }
            else
            {
                MessageBox.Show("请选择商品");
            }
        }
        private void 立即购买ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            //添加购买的识别变量设定1
            usergn2.type = 1;
            //取得用户选择的行号
            usergn2.index = this.dataGridView2.CurrentRow.Index;
            //调用修改画面
            usergn2 fupdate = new usergn2();
            fupdate.Show();
            //FoodID = dataGridView2.SelectedRows[0].Cells["Column6"].Value.ToString();
        }
        #endregion


        #region 购物车
        private void but_gwc_Click(object sender, EventArgs e)
        {
            //其他页面隐藏
            dataGridView1.Hide();
            dataGridView2.Hide();
            dataGridView4.Hide();
            panel6.Hide();
            dataGridView5.Hide();
            panel5.Hide();

            dataGridView3.Show();
            //获取当前登录账户的id
            string sqlsuerID = string.Format($"select UserID from [User] where Unumber='{userLogin.suserName}' and Pwd='{userLogin.spassWord}'");
            string UserID = DBHelper.ExecuteScalar(sqlsuerID).ToString();

            string sql = string.Format($"select  * from Shopping s join Food f on s.FoodID=f.FoodID where s.UserID='{UserID}'");
            DataSet ds = DBHelper.SelectByAdapter(sql);
            dt3 = ds.Tables[0];

            dataGridView3.AutoGenerateColumns = false;
            dataGridView3.DataSource = dt3;
        }
        private void 删除此商品ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult rlt = MessageBox.Show("确定要删除吗？", "删除确认", MessageBoxButtons.YesNo);
            if (rlt == DialogResult.Yes)
            {
                //数据库删除
                int number = dataGridView3.CurrentRow.Index;
                string sql = string.Format($"delete from Shopping where FoodID={dataGridView3.Rows[number].Cells["Column10"].Value}");
                bool result = DBHelper.GetNonQuery(sql);
                if (result)
                {
                    MessageBox.Show("删除成功");
                    dt3.Rows.RemoveAt(number);
                }
            }
        }
        private void 修改此商品数量ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            //添加修改的识别变量设定0
            usergn2.type = 0;
            //取得用户选择的行号
            usergn2.index = this.dataGridView3.CurrentRow.Index;
            //调用修改画面
            usergn2 fupdate = new usergn2();
            fupdate.Show();
        }
        private void 购买ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            //添加购买的识别变量设定1
            usergn2.type = 3;
            //取得用户选择的行号
            usergn2.index = this.dataGridView3.CurrentRow.Index;
            //调用修改画面
            usergn2 fupdate = new usergn2();
            fupdate.Show();
        }
        #endregion


        private void button1_Click_1(object sender, EventArgs e)
        {
            //其他页面隐藏
            dataGridView1.Hide();
            dataGridView3.Hide();
            dataGridView4.Hide();
            panel6.Hide();
            dataGridView5.Hide();
            panel5.Hide();


            if (textBox1.Text != "")
            {
                dataGridView2.Show();
                string sql = string.Format($"select * from Food f,Merchants m where f.MeID=m.MeID");
                DataSet ds = DBHelper.SelectByAdapter(sql);

                dt2 = ds.Tables[0];
                dataGridView2.AutoGenerateColumns = false;
                dataGridView2.DataSource = dt2;

                DataView dv = new DataView(dt2);
                dv.RowFilter = string.Format($"FoodName like '%{textBox1.Text}%'");
                dataGridView2.DataSource = dv;
            }
            else
            {
                MessageBox.Show("请输入你想吃的，可以马上找到哦！");
            }
        }//搜索菜品功能

        #region 订单表
        private void but_Order_Click(object sender, EventArgs e)//查询订单
        {
            //其他页面隐藏
            dataGridView1.Hide();
            dataGridView3.Hide();
            dataGridView2.Hide();
            panel6.Hide();
            dataGridView5.Hide();
            panel5.Hide();

            dataGridView4.Show();
            //获取当前登录账户的id
            string sqlsuerID = string.Format($"select UserID from [User] where Unumber='{userLogin.suserName}' and Pwd='{userLogin.spassWord}'");
            string UserID = DBHelper.ExecuteScalar(sqlsuerID).ToString();

            string sql = string.Format($"select * from [Order] o,Detailed d,Food f,[User] u where o.UserID=u.UserID and d.FoodID=f.FoodID and d.OrderID=o.OrderID and u.UserID='{UserID}'");
            DataSet ds = DBHelper.SelectByAdapter(sql);

            dt4 = ds.Tables[0];
            dataGridView4.AutoGenerateColumns = false;
            dataGridView4.DataSource = dt4;
        }
        private void 确认已送达ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int number = dataGridView4.CurrentRow.Index;
            string sql2 = ($"select OrderState from [Order] where OrderID='{dataGridView4.Rows[number].Cells["Column16"].Value}'");
            string ds = DBHelper.ExecuteScalar(sql2).ToString();
            if (ds == "接单中" || ds == "派送中" || ds=="等待接单")
            {
                DialogResult rlt = MessageBox.Show("确定已送达吗？", "提示：", MessageBoxButtons.YesNo);
                if (rlt == DialogResult.Yes)
                {
                    string sql = ($"update [Order] set OrderState='{"已送达"}' where OrderID='{dataGridView4.Rows[number].Cells["Column16"].Value}'");
                    bool result = DBHelper.GetNonQuery(sql);
                    if (result)
                    {
                        MessageBox.Show("确认成功！！");
                        //画面更新
                        //********************画面控件数据更新
                        //取得画面选中行
                        DataRow dr = usergn1.dt4.Rows[number];
                        //开始编辑
                        dr.BeginEdit();
                        dr["OrderState"] = "已送达";
                        dr.EndEdit();
                    }
                }
            }
            if (ds == "已送达")
            {
                MessageBox.Show("已送达，无须确认");
            }
        }
        private void 取消订单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int number = dataGridView4.CurrentRow.Index;
            string sql2 = ($"select OrderState from [Order] where OrderID='{dataGridView4.Rows[number].Cells["Column16"].Value}'");
            string ds = DBHelper.ExecuteScalar(sql2).ToString();
            if (ds == "接单中" || ds == "派送中" || ds=="等待接单")
            {
                DialogResult rlt = MessageBox.Show("确定取消订单吗？", "提示：", MessageBoxButtons.YesNo);
                if (rlt == DialogResult.Yes)
                {
                    string sql = ($"update [Order] set OrderState='{"等待取消"}' where OrderID='{dataGridView4.Rows[number].Cells["Column16"].Value}'");
                    bool result = DBHelper.GetNonQuery(sql);
                    if (result)
                    {
                        MessageBox.Show("等待商家取消");
                        //画面更新
                        //********************画面控件数据更新
                        //取得画面选中行
                        DataRow dr = usergn1.dt4.Rows[number];
                        //开始编辑
                        dr.BeginEdit();
                        dr["OrderState"] = "等待取消";
                        dr.EndEdit();
                    }
                }
            }
            if (ds == "已送达")
            {
                MessageBox.Show("已送达，不可取消");
            }
        }
        #endregion


        #region 个人信息查询与修改
        private void but_gr_Click(object sender, EventArgs e)
        {
            //画面隐藏
            dataGridView1.Hide();
            dataGridView3.Hide();
            dataGridView2.Hide();
            dataGridView4.Hide();
            panel5.Hide();
            panel6.Show();

            //dataGridView5.Show();
            //获取当前登录账户的id
            string sqlsuerID = string.Format($"select UserID from [User] where Unumber='{userLogin.suserName}' and Pwd='{userLogin.spassWord}'");
            string UserID = DBHelper.ExecuteScalar(sqlsuerID).ToString();

            lab_id.Text = UserID;
            lab_num.Text = userLogin.suserName;
            string sqlname = string.Format($"select Name from [User] where UserID='{UserID}'");
            string name = DBHelper.ExecuteScalar(sqlname).ToString();
            lab_name.Text = name;
            string sqlphone = string.Format($"select Phone from [User] where UserID='{UserID}'");
            string phone = DBHelper.ExecuteScalar(sqlphone).ToString();
            lab_phone.Text = phone;
            string sqlsex = string.Format($"select Sex from [User] where UserID='{UserID}'");
            string sex = DBHelper.ExecuteScalar(sqlsex).ToString();
            lab_sex.Text = sex;
            string sqladdress = string.Format($"select Address from [User] where UserID='{UserID}'");
            string address = DBHelper.ExecuteScalar(sqladdress).ToString();
            lab_address.Text = address;
            string sqlmoney = string.Format($"select Money from [User] where UserID='{UserID}'");
            string money = DBHelper.ExecuteScalar(sqlmoney).ToString();
            lab_qb.Text = money;
        }//个人信息查询
        private void but_xxxg_Click_1(object sender, EventArgs e)//调用修改画面  修改个人信息
        {
            //画面隐藏
            dataGridView1.Hide();
            dataGridView3.Hide();
            dataGridView2.Hide();
            dataGridView4.Hide();
            dataGridView5.Hide();
            panel5.Hide();

            //dataGridView5.Show();
            //获取当前登录账户的id
            string sqlsuerID = string.Format($"select UserID from [User] where Unumber='{userLogin.suserName}' and Pwd='{userLogin.spassWord}'");
            string UserID = DBHelper.ExecuteScalar(sqlsuerID).ToString();

            string sqlUser = string.Format($"select * from [User] where UserID='{UserID}'");
            DataSet ds = DBHelper.SelectByAdapter(sqlUser);

            dt5 = ds.Tables[0];
            dataGridView5.AutoGenerateColumns = false;
            dataGridView5.DataSource = dt5;
            //修改个人信息的识别变量设定4
            usergn2.type = 4;
            //取得用户选择的行号
            usergn2.index = this.dataGridView5.CurrentRow.Index;
            //调用修改画面
            usergn2 fupdate = new usergn2();
            fupdate.Show();
        }
        #endregion


        #region 个人钱包查询与充值
        private void button2_Click_1(object sender, EventArgs e)//查询钱包
        {
            usergn2.type = 5;
            usergn2 usergn = new usergn2();
            usergn.Show();
        }
        #endregion

        #region 关闭当前窗口和判断退出系统和退出当前账号
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)//关闭当前窗口和判断退出系统
        {
            if (gj == 0)
            {
                DialogResult rlt = MessageBox.Show("确认关闭本窗口吗？", "提示：", MessageBoxButtons.YesNo);
                if (rlt == DialogResult.Yes)
                {
                    userLogin userLogin = new userLogin();
                    userLogin.Show();
                }
                else
                {
                    e.Cancel = true;
                }
            }
            if (gj == 1)
            {
                DialogResult rlt = MessageBox.Show("确认退出系统吗？", "提示：", MessageBoxButtons.YesNo);
                if (rlt == DialogResult.Yes)
                {
                    System.Environment.Exit(0);
                }
                else
                {
                    e.Cancel = true;
                }

            }

        }
        private void button4_Click(object sender, EventArgs e)//退出系统
        {
            gj = 1;
            this.Close();
        }

        private void but_qhzh_Click(object sender, EventArgs e)//退出当前账号
        {
            gj = 0;
            this.Close();
        }
        #endregion


        private void timer1_Tick_1(object sender, EventArgs e)//当前时间
        {
            label2.Text = DateTime.Now.ToString("yyyy年MM月dd日hh时mm分ss秒");
        }

        
        private void button2_Click(object sender, EventArgs e)
        {

        }

        
    }
}
